/*
 * @Description: 
 * @Autor: Bingo
 * @Date: 2024-07-23 18:14:17
 * @LastEditors: Bingo
 * @LastEditTime: 2024-07-31 17:39:24
 */
import { LS } from '@/utils/common';
export function usePermissionDirective() {
  
  const checkPermission = (el: { parentNode: { removeChild: (arg0: any) => any; }; }, binding: { value: any; }) => {
    const requiredPermissions = binding.value;
    const hasPermission = checkUserPermissions(requiredPermissions);

    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  };

  const checkUserPermissions = (permissions: any[]=[]) => {
    const permissionList = LS.get('RIGHT_WORKS')
    // 用户有的权限list
    console.log('permissionList',permissionList)
    // 用户需要的权限
    console.log('permissions',permissions)
    // 这里应该实现你的权限检查逻辑，例如从 Vuex store 获取用户权限
    // 示例逻辑:
    let index = permissions.some(permission => permissionList.includes(permission+''))
    console.log('index',index)
    return index
  };

  return {
    mounted(el: { parentNode: { removeChild: (arg0: any) => any; }; }, binding: { value: any; }) {
      checkPermission(el, binding);
    },
    updated(el: { parentNode: { removeChild: (arg0: any) => any; }; }, binding: { value: any; }) {
      // 如果权限值改变，重新检查权限
      checkPermission(el, binding);
    }
  };
}

export default usePermissionDirective;